/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.chl.niclun.projectwebshop.nbp;

import edu.chl.niclun.projectwebshop.nbp.Entities.Movie;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.event.ActionEvent;

/**
 *Controllerbean for the ViewCart
 * @author niclas
 */
@ManagedBean(name="ViewCartControllerBean")
@RequestScoped
public class ViewCartControllerBean {

    @ManagedProperty(value="#{ViewCartBean}")
    private ViewCartBackingBean cartBean;
    
    @ManagedProperty(value="#{CartBean}")
    private CartModelBean cartModelBean;
    
    @ManagedProperty(value="#{LoginBean}")
    private LoginBean loginBean;
    
    /** Creates a new instance of ViewCartControllerBean */
    public ViewCartControllerBean() {
    }
    
    //update the ViewCart page
    @PostConstruct
    public void update() {
        cartBean.setCart(cartModelBean.getCart());
        cartBean.setCount(cartModelBean.getCount());
        cartBean.setTotalCost(cartModelBean.getTotalCost());
        cartBean.setShippingCost(cartModelBean.getShippingCost());
        cartBean.setTotCostIncShipping(cartModelBean.getTotCostIncShipping());
        cartBean.setMovieList(cartModelBean.getMovieList());
    }
    
    public ViewCartBackingBean getCartBean() {
        return cartBean;
    }
    
    public void setCartBean(ViewCartBackingBean cartBean) {
        this.cartBean = cartBean;
    }
    
    public CartModelBean getCartModelBean() {
        return cartModelBean;
    }
    
    public void setCartModelBean(CartModelBean cartModelBean) {
        this.cartModelBean = cartModelBean;
    }
    //add product
    public void addProduct(Movie m) {
        cartModelBean.addToCart(m);
        update();
    }
    //remove prodct
    public void removeProduct(Movie m) {
        cartModelBean.removeFromCart(m);
        update();
    }
    //emty cart
    public void emptyCart() {
        cartModelBean.emptyCart();
        update();
    }
    
    public void setLoginBean(LoginBean loginBean) {
        this.loginBean = loginBean;
    }
    
    public LoginBean getLoginBean() {
        return loginBean;
    }
    
    public String navigateToPay() {
        if(loginBean.isIsLoggedIn()) {
            if(cartModelBean.getCount() > 0) 
                return "pay";
            else
                return "";
        }else
            return "login";
    }
}
